﻿// -----------------------------------------------------------------------
// <copyright file="MonthRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MonthRepository : AbstractRepository<Month>
    {
        public MonthRepository()
        {
            base._contextHelper = null;
            base._context = new Context.AGRContext();
        }

        public override IQueryable<Month> GetAll()
        {
            var result = (from x in base._context.Months
                          select new Month
                          {
                              Id = x.Id,
                              MonthName = x.MonthName,
                              MonthNumber = x.MonthNumber,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Month entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Month entityToAdd = new Context.Month
            {
                MonthName = entity.MonthName,
                MonthNumber = entity.MonthNumber,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Months.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Month entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Month entityToUpdate = base._context.Months.Single(x => x.MonthName.Equals(entity.MonthName));

            entityToUpdate.MonthName = entity.MonthName;
            entityToUpdate.MonthNumber = entity.MonthNumber;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Month entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Month entityToDelete = base._context.Months.Single(x => x.MonthName.Equals(entity.MonthName));

            base._context.Months.DeleteOnSubmit(entityToDelete);
        }
    }
}
